package com.zz.client.handle.defaults;

import com.zz.common.annotation.Executor;
import com.zz.common.entity.Information;
import com.zz.common.enums.MessageType;
import com.zz.common.handle.executor.AbstractMessageExecutor;
import com.zz.common.protocol.DefaultProtocol;
import com.zz.common.tunnel.TunnelCache;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

/**
 * 心跳消息处理器
 */
@Slf4j
@Executor(msgType = MessageType.TYPE_HEARTBEAT)
public class HeartbeatExecutor extends AbstractMessageExecutor {
    @Override
    public void execute(Information information) {
        ChannelHandlerContext context = information.getContext();
        DefaultProtocol defaultProtocolMsg = (DefaultProtocol) information.getProtocolMsg();
        // 对于服务端发送的心跳，不做重复回复
        if (TunnelCache.isReplyHeartbeat(context.channel(), defaultProtocolMsg.getSerialNumber())) {
            return;
        }
        DefaultProtocol protocol = DefaultProtocol.builder().type(MessageType.TYPE_HEARTBEAT)
                .serialNumber(defaultProtocolMsg.getSerialNumber())
                .build();
        log.debug("response heartbeat message {}", context.channel());
        context.channel().writeAndFlush(protocol);
    }


}
